//
//  ISDUETTableNewsViewController.m
//  12Cung
//
//  Created by Tran Bao Thai on 8/7/12.
//  Copyright (c) 2012 Tran Bao Thai. All rights reserved.
//

#import "ISDUETTableNewsViewController.h"
#import "UIImageView+WebCache.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "JSON.h"
#import "MBProgressHUD.h"
#import "ISDUETNewsItem.h"
#import "Constants.h"
#import "ISDUETNewsDetailViewController.h"

@interface ISDUETTableNewsViewController (){
    
}

@end

@implementation ISDUETTableNewsViewController
@synthesize tableData;
@synthesize newsTableView;
@synthesize lblTitle;
@synthesize typeOfNew;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        tableData = [[NSMutableArray alloc]initWithCapacity:5];
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    
    if (tableData.count <= 0) {
        // Get news list
        NSURL* serverUrl = [NSURL URLWithString:SERVER_SIDE];
        ASIFormDataRequest* request = [ASIFormDataRequest requestWithURL:serverUrl];
        [request setPostValue:typeOfNew forKey:TABLE_NAME_POST];
        [request setPostValue:@"0" forKey:FROM_POST];
        [request setPostValue:@"2" forKey:LENGTH_POST];
        [request setDelegate:self];
        [request startAsynchronous];
        
        MBProgressHUD* hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hub.labelText = @"Loading...";
    }
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    NSString* responseString = [request responseString];
    NSArray* resultArr = [responseString JSONValue];
    for (NSDictionary* dictItem in resultArr) {
        ISDUETNewsItem* newsItem = [[ISDUETNewsItem new] autorelease];
        newsItem.title = [[NSString alloc] initWithString:[dictItem objectForKey:KEY_TITLE]];
        newsItem.introduce = [[NSString alloc] initWithString:[dictItem objectForKey:KEY_INTRODUCE]];
        newsItem.image = [[NSString alloc] initWithString:[dictItem objectForKey:KEY_IMAGE]];
        newsItem.detailLink = [[NSString alloc] initWithString:[dictItem objectForKey:KEY_DETAIL_LINK]];
        newsItem.publisDateStr = [[NSString alloc] initWithString:[dictItem objectForKey:KEY_PUBLIC_DATE]];
        [newsItem showDetail];
        [tableData addObject:newsItem];
    }
    [newsTableView reloadData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    NSError *error = [request error];
    NSLog(@"ERROR: %@", error.localizedDescription);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBarHidden = YES;
    if (typeOfNew == HD_TINTUC) {
        lblTitle.text = @"Tin tức";
    } else if(typeOfNew == HD_TVHN){
        lblTitle.text = @"Tử vi hàng ngày";
    }
    googleBannerView = [[GADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - GAD_SIZE_320x50.height, GAD_SIZE_320x50.width, GAD_SIZE_320x50.height)];
    googleBannerView.adUnitID = @"a14f183b45ada4f";
    googleBannerView.rootViewController = self;
    [self.view addSubview:googleBannerView];
    [googleBannerView loadRequest:[GADRequest request]];
}

- (void)viewDidUnload
{
    [self setNewsTableView:nil];
    //tableData = nil;
    [self setLblTitle:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    //[tableData release];
    //tableData = nil;
    [typeOfNew release];
    [newsTableView release];
    [lblTitle release];
    [super dealloc];
}

// tableview
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	return [tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CustomNewsCell"];
	if(!cell){
		NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomNewsCell" owner:self options:nil];
        cell = [topLevelObjects objectAtIndex:0];
	}
    if (indexPath.row >= tableData.count) {
        return nil;
    }
    
	UIImageView * newsImage = (UIImageView *)[cell viewWithTag:3];
	UILabel * newsTitle = (UILabel *)[cell viewWithTag:1];
	UILabel * newsIntroduce = (UILabel *)[cell viewWithTag:2];
    ISDUETNewsItem* newsItem = (ISDUETNewsItem*)[tableData objectAtIndex:indexPath.row];
    [newsTitle setText:newsItem.title];
    [newsIntroduce setText:newsItem.introduce];
    [newsImage setImageWithURL:[NSURL URLWithString:@"http://inil.me/thaitb/hoangdao/hdimages/wtt-bach-duong.jpg"]];

	return cell;
}

#pragma UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ISDUETNewsDetailViewController* detailVC = [[[ISDUETNewsDetailViewController alloc]initWithNibName:@"ISDUETNewsDetailViewController" bundle:nil]autorelease];
    detailVC.newsItem = [[tableData objectAtIndex:indexPath.row]retain];
    detailVC.typeOfNew = [self.typeOfNew retain];
    [self.navigationController pushViewController:detailVC animated:YES];
}
- (IBAction)backAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
